#/*
# * Copyright {c} 2020-2021, SERI Development Team
# *
# * SPDX-License-Identifier: Apache-2.0
# *
# * Change Logs:
# * Date         Author          Notes
# * 2022-03-27   Lyons           first version
# */

TARGET          = riscv

Q               = @

PROJPATH        = ../..
WORKPATH        = .

DEMO ?= rtos

include ../config.mk

## bootloader

INCLUDES_BOOT   = ${INCLUDES}
INCFILES_BOOT   = ${INCFILES}
CFLAGS_BOOT     = ${CFLAGS}
LDFLAGS_BOOT    = ${LDFLAGS}
LDLIBS_BOOT     = ${LDLIBS}
ASMFILES_BOOT   = ${ASMFILES}
CFILES_BOOT     = ${CFILES}

INCLUDES_BOOT  += 

INCFILES_BOOT  += 

CFLAGS_BOOT    += -O0 -ffunction-sections -fdata-sections
# CFLAGS_BOOT    += -g
CFLAGS_BOOT    += -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast
# CFLAGS_BOOT    += -DPRINT_STDIO_SIM

LDFLAGS_BOOT   += -T${WORKPATH}/link_boot.lds

LDLIBS_BOOT    += 

ASMFILES_BOOT  += 

CFILES_BOOT    += ${WORKPATH}/main_boot.c

## application

INCLUDES_APP    = ${INCLUDES}
INCFILES_APP    = ${INCFILES}
CFLAGS_APP      = ${CFLAGS}
LDFLAGS_APP     = ${LDFLAGS}
LDLIBS_APP      = ${LDLIBS}
ASMFILES_APP    = ${ASMFILES}
CFILES_APP      = ${CFILES}

INCLUDES_APP   += -I.

INCFILES_APP   += -I${WORKPATH}/rtthread-nano/bsp \
                  -I${WORKPATH}/rtthread-nano/components/finsh \
                  -I${WORKPATH}/rtthread-nano/include \
                  -I${WORKPATH}/rtthread-nano/include/libc \
                  -I${WORKPATH}/libs/_kernel/libcpu/risc-v/common

INCFILES_APP   += -I${WORKPATH}/driver

CFLAGS_APP     += -O0 -ffunction-sections -fdata-sections
# CFLAGS_APP     += -g
CFLAGS_APP     += -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast
ifeq ($(DEMO), rtos)
CFLAGS_APP     += -DTEST_RTOS
CFLAGS_APP     += -DOS_ENABLE_RT_THREAD
# CFLAGS_APP     += -DPRINT_STDIO_SIM
CFLAGS_APP     += -DSIMULATION_PRINT_VERSION
endif
ifeq ($(DEMO), uart)
CFLAGS_APP     += -DTEST_UART
endif
ifeq ($(DEMO), lcd)
CFLAGS_APP     += -DTEST_LCD
endif
# CFLAGS_APP     += -DSIMULATION_PRINT_THREAD_SWITCH

LDFLAGS_APP    += -T${WORKPATH}/link_app.lds

LDLIBS_APP     += 

ASMFILES_APP   += ${WORKPATH}/rtthread-nano/libcpu/risc-v/common/context_gcc.S

ifeq ($(DEMO), rtos)
CFILES_APP     += ${WORKPATH}/rtthread-nano/bsp/board.c \
                  ${WORKPATH}/rtthread-nano/components/device/device.c \
                  ${WORKPATH}/rtthread-nano/components/finsh/cmd.c \
                  ${WORKPATH}/rtthread-nano/components/finsh/finsh_port.c \
                  ${WORKPATH}/rtthread-nano/components/finsh/msh.c \
                  ${WORKPATH}/rtthread-nano/components/finsh/shell.c \
                  ${WORKPATH}/rtthread-nano/libcpu/risc-v/common/cpuport.c \
                  ${WORKPATH}/rtthread-nano/src/clock.c \
                  ${WORKPATH}/rtthread-nano/src/components.c \
                  ${WORKPATH}/rtthread-nano/src/cpu.c \
                  ${WORKPATH}/rtthread-nano/src/idle.c \
                  ${WORKPATH}/rtthread-nano/src/ipc.c \
                  ${WORKPATH}/rtthread-nano/src/irq.c \
                  ${WORKPATH}/rtthread-nano/src/kservice.c \
                  ${WORKPATH}/rtthread-nano/src/mem.c \
                  ${WORKPATH}/rtthread-nano/src/memheap.c \
                  ${WORKPATH}/rtthread-nano/src/mempool.c \
                  ${WORKPATH}/rtthread-nano/src/object.c \
                  ${WORKPATH}/rtthread-nano/src/scheduler.c \
                  ${WORKPATH}/rtthread-nano/src/slab.c \
                  ${WORKPATH}/rtthread-nano/src/thread.c \
                  ${WORKPATH}/rtthread-nano/src/timer.c
endif

ifeq ($(DEMO), lcd)
CFILES_APP     += ${WORKPATH}/driver/Driver_tftspi.c \
                  ${WORKPATH}/driver/Task_tft.c
endif

CFILES_APP     += ${WORKPATH}/init.c \
                  ${WORKPATH}/interrupt.c \
                  ${WORKPATH}/main_app.c

COLORS = "\033[32m"
COLORE = "\033[0m"

.PHONY: help
help:
	@echo "help     - help menu                                 "
	@echo "build    - compile c/asm file                        "

.PHONY: build_boot
build_boot:
ifneq ($(shell uname), Linux)
	@echo -e ${COLORS}[INFO] compile bootloader c/asm file ...${COLORE}
	${Q}${CC} ${INCLUDES_BOOT} ${INCFILES_BOOT} ${CFLAGS_BOOT} ${LDFLAGS_BOOT} ${LDLIBS_BOOT} ${ASMFILES_BOOT} ${CFILES_BOOT} -o ${TARGET}_boot.elf
	@echo -e ${COLORS}[INFO] create bootloader dump file ...${COLORE}
	${Q}${OBJDUMP} -D -S ${TARGET}_boot.elf > ${TARGET}_boot.dump
	@echo -e ${COLORS}[INFO] create bootloader image file ...${COLORE}
	${Q}${OBJCOPY} -S -O binary  ${TARGET}_boot.elf ${TARGET}_boot.bin
	${Q}${PROJPATH}/scripts/bin2mi.exe ${TARGET}_boot.bin
	${Q}${CP} rom.mi ${PROJPATH}/riscv-bootloader.mi
endif
	@echo -e ${COLORS}[INFO] execute done${COLORE}

.PHONY: build_app
build_app:
ifneq ($(shell uname), Linux)
	@echo -e ${COLORS}[INFO] compile application c/asm file ...${COLORE}
	${Q}${CC} ${INCLUDES_APP} ${INCFILES_APP} ${CFLAGS_APP} ${LDFLAGS_APP} ${LDLIBS_APP} ${ASMFILES_APP} ${CFILES_APP} -o ${TARGET}_app.elf
	@echo -e ${COLORS}[INFO] create application dump file ...${COLORE}
	${Q}${OBJDUMP} -D -S ${TARGET}_app.elf > ${TARGET}_app.dump
	@echo -e ${COLORS}[INFO] create application image file ...${COLORE}
	${Q}${OBJCOPY} -S -O binary  ${TARGET}_app.elf ${TARGET}_app.bin
	${Q}${CP} ${TARGET}_app.bin ${PROJPATH}/riscv-application.bin
endif
	@echo -e ${COLORS}[INFO] execute done${COLORE}

.PHONY: build
build: build_boot build_app

.PHONY: clean
clean:
	@echo -e ${COLORS}[INFO] clean project ...${COLORE}
	@make -C ${WORKPATH}/../xemu clean
	@ls | grep -vE "Makefile|driver|rtthread-nano|*\.lds$$|*\.c$$|*\.h$$" | xargs -i rm -rf {}
	@echo -e ${COLORS}[INFO] execute done${COLORE}
